<?php
/*
Plugin Name: Author Box After Posts
Plugin URI: http://www.92app.com/wordpress-plugins/author-box-after-posts
Description:Adds an author box after your post contents.The box contains author's avatar,name,post count,site link,personal description and Email.
Version: 1.2
Author: Jeriff Cheng
Author URI: http://www.92app.com/
*/

/*
Copyright 2011  Jeriff Cheng(Email : hschengyongtao@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
*/
		/*
			Contents:
		1.Add Facebook,Google,Twitter to User Contact Fields
		2.Add CSS Style Sheet
		3.Output of the Author Box
		*/

################################################################
##### 1. Add Facebook,Google,Twitter to User Contact Fields#####
################################################################		
function add_twitter_contactmethod( $contactmethods ) {
  $contactmethods['twitter'] = 'Twitter';
  $contactmethods['facebook'] = 'Facebook';
  $contactmethods['google_profile'] = 'Google Profile';
  return $contactmethods;
}
add_filter('user_contactmethods','add_twitter_contactmethod',10,1);

################################################################
######################### 2.Add CSS Style Sheet#################
################################################################	
function add_authorbox_css() {
    ?>
<link rel="stylesheet" href="<?php echo get_option('siteurl') . '/' . PLUGINDIR . '/' . dirname(plugin_basename (__FILE__))?>/authorboxafterposts.css" type="text/css" media="screen" />
    <?php
}
add_action(wp_head,add_authorbox_css,1);

################################################################
############### 3.Output of the Author Box#######################
################################################################
add_filter('the_content', 'add_author_box');

function add_author_box($content) {
	//Define the Main Part of Author Box


        query_posts(array('author' => get_the_author_id(), 'category__in' => array(7,15,14,20,21,24,19,18,32,31,22,26,29,34,16,23,17,33,27,30,28,26)));

	global $wp_query;
	if ( $wp_query->found_posts > 0 ) {
		$author_box .= '';

		while ( have_posts() ) {
			the_post();
			$author_box .= '<br><hr />  
<br><img class="alignnone size-thumbnail wp-image-2517" title="paypal_logo" src="http://www.wonderbookland.com/wp-content/uploads/2012/07/paypal_logo-150x125.jpg" alt="" width="150" height="125" /><br><br>
Click link to read <a title="Site Sales Disclaimer" href="/disclaimer/">disclaimer</a>';
		}
	}
	wp_reset_query();


	$author_box='<br />';
      
	query_posts('author='.get_the_author_id().'&category__in=6');

	global $wp_query;

        if ( $wp_query->found_posts > 0 ) {
		$author_box .= '<br /><h4>Author:</h4>';

		while ( have_posts() ) {
			the_post();
			$author_box .= '<a href="'.get_permalink(get_the_ID()).'">'.get_the_title(get_the_ID()).'</a><br />';
		}
	}
         
        
	wp_reset_query();


	query_posts(array('author' => get_the_author_id(), 'category__in' => array(7,15,14,20,21,24,19,18,32,31,22,26,29,34,16,23,17,33,27,30,28,26)));

	global $wp_query;
	if ( $wp_query->found_posts > 0 ) {
		$author_box .= '<br /><h4>Books:</h4>';

		while ( have_posts() ) {
			the_post();
			$author_box .= '<a href="'.get_permalink(get_the_ID()).'">'.get_the_title(get_the_ID()).'</a><br />';
		}
	}
	wp_reset_query();

        query_posts('author='.get_the_author_id().'&category__in=42');

	global $wp_query;

	if ( $wp_query->found_posts > 0 ) {
		$author_box .= '<br /><h4>Book Excerpts:</h4>';

		while ( have_posts() ) {
			the_post();
			$author_box .= '<a href="'.get_permalink(get_the_ID()).'">'.get_the_title(get_the_ID()).'</a><br />';
		}
	}
	wp_reset_query();

        query_posts('author='.get_the_author_id().'&category__in=37');

	global $wp_query;

	if ( $wp_query->found_posts > 0 ) {
		$author_box .= '<br /><h4>Poems:</h4>';

		while ( have_posts() ) {
			the_post();
			$author_box .= '<a href="'.get_permalink(get_the_ID()).'">'.get_the_title(get_the_ID()).'</a><br />';
		}
	}
	wp_reset_query();

        query_posts('author='.get_the_author_id().'&category__in=39');

	global $wp_query;

	if ( $wp_query->found_posts > 0 ) {
		$author_box .= '<br /><h4>Short Stories:</h4>';

		while ( have_posts() ) {
			the_post();
			$author_box .= '<a href="'.get_permalink(get_the_ID()).'">'.get_the_title(get_the_ID()).'</a><br />';
		}
	}
        
	wp_reset_query();

        query_posts('author='.get_the_author_id().'&category__in=108');

	global $wp_query;

	if ( $wp_query->found_posts > 0 ) {
		$author_box .= '<br /><h4>Articles:</h4>';

		while ( have_posts() ) {
			the_post();
			$author_box .= '<a href="'.get_permalink(get_the_ID()).'">'.get_the_title(get_the_ID()).'</a><br />';
		}
	}
        
	wp_reset_query();

        
	//Dynamic Output of the Author Box (Show Info you've set)
	// if ( is_single() )

	
	global $post;
	global $wpdb;
	$mk_sql = "SELECT * " ."FROM ".$wpdb->prefix."wp_eMember_members_tbl WHERE user_name = '".get_the_author_meta('display_name')."'";
	$result = $wpdb->get_results($mk_sql , ARRAY_A);

	//if in category 6 or post type member_pages or post type bookclub
	$authorid = $result[0]['member_id'];
	$content .= mk_eshop_show_user_product($post->ID,$authorid);
	$content.= $author_box;
	if(in_category(6)){
		$content.= mk_add_author_box_mingle_link(get_the_author_meta('display_name'));
	}elseif(get_post_type($post->ID) == 'member_pages'){
		$content.= mk_add_author_box_mingle_link(get_the_author_meta('display_name'));
	}elseif(get_post_type($post->ID) == 'bookclub'){
		$content.= mk_add_author_box_mingle_link(get_the_author_meta('display_name'));
		$content.= "<div id = 'mk_load_mingle_profile' class='michael'></div>";
		$content.= "<script type='text/javascript' src='".plugins_url( 'author-box-after-posts/loadprofile.js?ver=1.6' , dirname(__FILE__) )."'></script>";
	}
	
	return $content;
	

echo "\n";

}

function mk_add_author_box_mingle_link($authorname){
	if(is_plugin_active('mingle/mingle.php')){
		global $user_ID;
		$MnglUser = get_userdata($user_ID);
		global $mngl_options;
		$myProfURL2 = '';
		
		if(isset($mngl_options->profile_page_id) and $mngl_options->profile_page_id != 0){
			if( MnglUtils::rewriting_on() and $mngl_options->pretty_profile_urls ){
				global $mngl_blogurl;
				$struct = MnglUtils::get_permalink_pre_slug_uri();
				// $myProfURL2 = "{$mngl_blogurl}{$struct}{$MnglUser->user_login}";
				$myProfURL2 = "{$mngl_blogurl}{$struct}{$authorname}";
				
			}else{
				$permalink = get_permalink($mngl_options->profile_page_id);
				$param_char = ((preg_match("#\?#",$permalink))?'&':'?');
				// $myProfURL2 = "{$permalink}{$param_char}u={$MnglUser->user_login}";
				$myProfURL2 = "{$permalink}{$param_char}u={$authorname}";
			}

		}
		
		
		$image = "http://www.wonderbookland.com/wp-content/uploads/2012/07/arrow_small.png";  
		$width = 18;
		$height = 18;
		
		
		// $link = '<br /><img src="'.$image.'" style="width:'.$width.'; height:'.$height.';" />';
		$link .= "<a id='user_button' style='display:none' href='" . $myProfURL2 . "' title='".__("View community profile", "mingleforum")."'>".__("View community profile", "mingleforum")."</a>";
	}else{
		$image = "http://www.wonderbookland.com/wp-content/uploads/2012/07/arrow_small.png";  
		$width = 18;
		$height = 18;
		
		// $link = '<br /><img src="'.$image.'" style="width:'.$width.'; height:'.$height.';" />';
		$link .= "<a id='user_button' style='display:none' href='".$this->base_url."profile&id=$user_ID' title='".__("View community profile", "mingleforum")."'>".__("View community profile", "mingleforum")."</a>";
	}
	
	return $link;
}

//add author product
function mk_eshop_show_user_product($postid,$authorid){
	$show_product_short_code = '';
	if(get_post_type($postid) == 'member_pages' || in_category(6)){
		$user_products = get_option('dk_eshop_user_products_' . $authorid);
		foreach($user_products as $key => $value){
			$show_product_short_code .='[wp_eStore_fancy2 id='.$value.']';
			// $show_product_short_code .= '[wp_eStore_add_to_cart id='.$value.']';
			
		}
	}
	return $show_product_short_code;
}


//function user profile
function mk_user_profile(){
?>
<?php global $mngl_user, $mngl_friend, $mngl_options; ?>
<?php $display_profile = ( $user->privacy == 'public' or 
                           MnglUser::is_logged_in_and_an_admin() or 
                           MnglUser::is_logged_in_and_visible() ); ?>

<p style="text-align: left;"><a href="/new-messages/" rel="attachment wp-att-1944"><img class="alignnone size-full wp-image-1944" title="Messages" src="http://www.wonderbookland.com/wp-content/uploads/2012/06/Messages1.png" alt="" width="104" height="34" /></a><a href="/community-notices/" rel="attachment wp-att-1945"><img class="alignnone size-full wp-image-1945" title="Notices" src="http://www.wonderbookland.com/wp-content/uploads/2012/06/Notices1.png" alt="" width="104" height="34" /></a><a href="/friend-requests/" rel="attachment wp-att-1946"><img class="alignnone size-full wp-image-1946" title="Requests" src="http://www.wonderbookland.com/wp-content/uploads/2012/06/Requests1.png" alt="" width="104" height="34" /></a><a href="/friends-and-buddies/" rel="attachment wp-att-1942"><img class="alignnone size-full wp-image-1942" title="Friends" src="http://www.wonderbookland.com/wp-content/uploads/2012/06/Friends1.png" alt="" width="104" height="34" /></a><a href="/community-directory/" rel="attachment wp-att-1943"><img class="alignnone size-full wp-image-1943" title="Directory" src="http://www.wonderbookland.com/wp-content/uploads/2012/06/Directory.png" alt="" width="104" height="34" /></a></p>
<table class="mngl-profile-table">
  <tr>
    <td valign="top" class="mngl-profile-table-col-1 mngl-valign-top">
      <table>
        <tr>
          <td>
            <?php echo $avatar; ?>
            <?php echo $mngl_friends_controller->display_add_friend_button($mngl_user->id, $user->id); ?>
            <?php echo do_action('mngl-profile-display',$user->id); ?>
          </td>
        </tr>
        <tr>
          <td valign="top" class="mngl-valign-top">
            <?php if($display_profile) { ?>

              <?php if(isset($mngl_options->field_visibilities['profile_front']['bio']) and !empty($user->bio)) { ?>
                <p class="mngl-profile-bio"><?php echo MnglBoardsHelper::format_message($user->bio); ?></p>
              <?php } ?>
              <div class="mngl-profile-information">
              <?php if(isset($mngl_options->field_visibilities['profile_front']['name']) and !empty($user->first_name) and ($user->first_name != $user->screenname)) { ?>
                <p class="mngl-profile-field"><strong><?php _e('Name', 'mingle'); ?>:</strong><br/><?php echo wptexturize(stripslashes($user->first_name)); ?>
                <?php if(!empty($user->last_name)){ ?>
                <?php echo " " . wptexturize(stripslashes($user->last_name)); ?>
                <?php } ?>
                </p>
              <?php } ?>
              <?php if(isset($mngl_options->field_visibilities['profile_front']['sex']) and !empty($user->sex)) { ?>
                <p class="mngl-profile-sex"><strong><?php _e('Gender', 'mingle'); ?>:</strong><br/><?php echo $user->sex_display; ?></p>
              <?php } ?>
              <?php if(isset($mngl_options->field_visibilities['profile_front']['location']) and !empty($user->location)) { ?>
                <p class="mngl-profile-location"><strong><?php _e('Location', 'mingle'); ?>:</strong><br/><?php echo wptexturize($user->location); ?></p>
              <?php } ?>
              <?php if(isset($mngl_options->field_visibilities['profile_front']['birthday']) and !empty($user->birthday)) { ?>
                <p class="mngl-profile-location"><strong><?php _e('Birthday', 'mingle'); ?>:</strong><br/><?php echo wptexturize($user->birthday); ?></p>
              <?php } ?>
              <?php if(isset($mngl_options->field_visibilities['profile_front']['url']) and !empty($user->url)) { ?>
                <p class="mngl-profile-url"><strong><?php _e('Website', 'mingle'); ?>:</strong><br/><?php echo make_clickable($user->url); ?></p>
              <?php } ?>
              </div>
            <?php } ?>
            <p><strong><?php _e('Friends', 'mingle'); ?>:</strong><div class="mngl-profile-friend-grid-wrap"><?php echo $mngl_friends_controller->display_friends_grid($user->id); ?></div></p>
<p class="mngl-user-grid-bookshelf"><br><a href="<?php echo DK_MNGL_BOOKSHELF_URL ?>?ig=iga&mguserid=<?php echo $user->id ;?> "><img src="../images/view_bookshelf.png" alt="Click to view and share books with your friends" /></a><br></br></p>    
 </td>
        </tr>
      </table>
    </td>
    <td valign="top" class="mngl-profile-table-col-2">
      <table class="mngl-profile-body">
        <tr>
          <td>
            
            <?php 
              if(!$display_profile)
                require( MNGL_VIEWS_PATH . '/mngl-boards/private.php' );
            ?>
          </td>
        </tr>
        <tr>
          <td>
          </td>
        </tr>
        <tr>
          <?php if($display_profile) { ?>
            <td valign="top" width="100%"><div class="mngl-board"><?php echo $mngl_boards_controller->display($user->id); ?></div></td>
          <?php } ?>
        </tr>
      </table>
    </td>
</table>



<?php
}
?>